#include "OpenglAppFramework/inc/SceneConfig.h"
#include "OpenglAppFramework/inc/PlugIn/Material.h"
#include "OpenglAppFramework/inc/PlugIn/Camera.h"
#include <iostream>

static const nlohmann::json default_transform_location  = {0, 0, 0};
static const nlohmann::json default_transform_scale     = {1, 1, 1};
static const nlohmann::json default_transform_rotation_aix = {0, 0, 1};
static const nlohmann::json default_transform_angle     = 0;
static const nlohmann::json default_transform = {
    {"location", default_transform_location},
    {"scale", default_transform_scale},
    {"rotation_aix", default_transform_rotation_aix},
    {"rotation_angle", default_transform_angle}
};

void addDefaultValue(nlohmann::json& val) {
    for(auto& it : val["game_objects"].items()) {
        if(it.value().find("transform") == it.value().end()) {
            it.value()["transform"] = default_transform;
            continue;
        }
        nlohmann::json& trans = it.value()["transform"];
        if(trans.find("location") == trans.end()) {
            trans["location"] = default_transform_location;
        }
        if(trans.find("scale") == trans.end()) {
            trans["scale"] = default_transform_scale;
        }
        if(trans.find("rotation_aix") == trans.end()) {
            trans["rotation_aix"] = default_transform_rotation_aix;
        }
        if(trans.find("rotation_angle") == trans.end()) {
            trans["rotation_angle"] = default_transform_angle;
        }
    }
}

void json2TransformInfo(const nlohmann::json& val, TransformInfo& trans) {
    const nlohmann::json& location = val["location"];
    trans.location = glm::vec3(location[0], location[1], location[2]);
    const nlohmann::json& scale = val["scale"];
    trans.scale = glm::vec3(scale[0], scale[1], scale[2]);
    const nlohmann::json& rot_aix = val["rotation_aix"];
    trans.rotation_axis = glm::vec3(rot_aix[0], rot_aix[1], rot_aix[2]);
    trans.rotation_angle = val["rotation_angle"];
}

void json2TransformInfoWithDefault(const nlohmann::json& val, TransformInfo& trans) {
    trans.location = val.find("location") == val.end() ? glm::vec3(0, 0, 0) : 
        glm::vec3(val["location"][0], val["location"][1], val["location"][2]);
    trans.scale = val.find("scale") == val.end() ? glm::vec3(1, 1, 1) : 
        glm::vec3(val["scale"][0], val["scale"][1], val["scale"][2]);
    trans.rotation_axis = val.find("rotation_aix") == val.end() ? glm::vec3(0, 1, 0) : 
        glm::vec3(val["rotation_aix"][0], val["rotation_aix"][1], val["rotation_aix"][2]);
    trans.rotation_angle = val.find("rotation_angle") == val.end() ? 0 : val["rotation_angle"].get<float>();
}

void json2Vec3(const nlohmann::json& js, glm::vec3& v) {
    v[0] = js[0].get<float>();
    v[1] = js[1].get<float>();
    v[2] = js[2].get<float>();
}

